/*
 * Copyright Ekeo(c) 2013.
 */

package com.intexh.quickandroid.module.chat.ui.face;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.util.Log;

import com.intexh.quickandroid.module.chat.ChatHelper;

import java.util.HashMap;


/**
 * @author: Milk <249828165@qq.com>
 * @since: 13-10-31
 */
public class EmojiFactory {
    private final static HashMap<String, Integer> idCache = new HashMap<String, Integer>();
    public final static String[] codePoints = new String[]{
            "1f60a", "1f60d", "1f618", "1f633", "1f621", "1f613", "1f632", "1f62d", "1f601", "1f631", "1f616"
            , "1f609", "1f60f", "1f61c", "1f630", "1f622", "1f61a", "1f604", "1f62a", "1f623", "1f614", "1f620"
            , "1f60c", "1f61d", "1f602", "1f625", "1f603", "1f628", "1f612", "1f637", "1f61e", "1f47f", "1f47d"
            , "1f444", "2764", "1f494", "1f498", "1f49d", "1f4a9", "1f44d", "1f44e", "1f44a", "270c", "1f44c"
            , "1f4aa", "1f446", "1f447", "1f448", "1f449", "1f467", "1f466", "1f469", "1f468", "1f491", "1f48f"
            , "1f47c", "1f480", "1f431", "1f436", "1f42d", "1f439", "1f430", "1f43a", "1f438", "1f42f", "1f428"
            , "1f43b", "1f437", "1f42e", "1f417", "1f435", "1f434", "1f40d", "1f426", "1f414", "1f427", "1f41b"
            , "1f419", "1f420", "1f433", "1f42c", "2600", "2614", "1f319", "2728", "2b50", "26a1", "2601", "26c4"
            , "1f30a", "2757", "2753", "1f4a4", "1f4a6", "1f3b5", "1f525", "1f339", "1f33a", "1f334", "1f335"
            , "1f385", "1f47b", "1f383", "1f384", "1f514", "1f389", "1f388", "1f4bf", "1f4f7", "1f3a5", "1f4bb"
            , "1f4fa", "1f513", "1f512", "1f511", "1f4a1", "1f4ec", "1f6c0", "1f4b0", "1f52b", "1f48a", "1f4a3"
            , "26bd", "1f3c8", "1f3c0", "1f3c6", "1f47e", "1f3a4", "1f3b8", "1f459", "1f451", "1f302", "1f45c"
            , "1f484", "1f48d", "1f381", "1f48e", "2615", "1f382", "1f37a", "1f37b", "1f378", "1f354", "1f35f"
            , "1f35d", "1f363", "1f35c", "1f366", "1f34e", "1f680", "1f684", "1f6b2", "1f3c1", "1f6b9", "1f6ba"
            , "2b55", "274c"
    };
    public final static String[] allEmojiCodePoints = new String[]{
            "1f302", "1f386", "1f40c", "1f463", "1f4b8", "1f512", "1f646", "23f3",
            "1f30a", "1f387", "1f40d", "1f464", "1f4b9", "1f513", "1f647", "2600",
            "1f319", "1f388", "1f40e", "1f465", "1f4ba", "1f514", "1f648", "2601",
            "1f330", "1f389", "1f40f", "1f466", "1f4bb", "1f515", "1f649", "260e",
            "1f331", "1f38a", "1f410", "1f467", "1f4bc", "1f516", "1f64a", "2611",
            "1f332", "1f38b", "1f411", "1f468", "1f4bd", "1f517", "1f64b", "2614",
            "1f333", "1f38c", "1f412", "1f469", "1f4be", "1f525", "1f64c", "2615",
            "1f334", "1f38d", "1f413", "1f46a", "1f4bf", "1f526", "1f64d", "261d",
            "1f335", "1f38e", "1f414", "1f46b", "1f4c0", "1f527", "1f64e", "263a",
            "1f337", "1f38f", "1f415", "1f46c", "1f4c1", "1f528", "1f64f", "2648",
            "1f338", "1f390", "1f416", "1f46d", "1f4c2", "1f529", "1f680", "2649",
            "1f339", "1f391", "1f417", "1f46e", "1f4c3", "1f52a", "1f681", "264a",
            "1f33a", "1f392", "1f418", "1f46f", "1f4c4", "1f52b", "1f682", "264b",
            "1f33b", "1f393", "1f419", "1f470", "1f4c5", "1f52c", "1f683", "264c",
            "1f33c", "1f3a0", "1f41a", "1f471", "1f4c6", "1f52d", "1f684", "264d",
            "1f33d", "1f3a1", "1f41b", "1f472", "1f4c7", "1f52e", "1f685", "264e",
            "1f33e", "1f3a2", "1f41c", "1f473", "1f4c8", "1f52f", "1f686", "264f",
            "1f33f", "1f3a3", "1f41d", "1f474", "1f4c9", "1f530", "1f687", "2650",
            "1f340", "1f3a4", "1f41e", "1f475", "1f4ca", "1f531", "1f688", "2651",
            "1f341", "1f3a5", "1f41f", "1f476", "1f4cb", "1f600", "1f689", "2652",
            "1f342", "1f3a6", "1f420", "1f477", "1f4cc", "1f601", "1f68a", "2653",
            "1f343", "1f3a7", "1f421", "1f478", "1f4cd", "1f602", "1f68c", "2660",
            "1f344", "1f3a8", "1f422", "1f479", "1f4ce", "1f603", "1f68d", "2663",
            "1f345", "1f3a9", "1f423", "1f47a", "1f4cf", "1f604", "1f68e", "2665",
            "1f346", "1f3aa", "1f424", "1f47b", "1f4d0", "1f605", "1f68f", "2666",
            "1f347", "1f3ab", "1f425", "1f47c", "1f4d1", "1f606", "1f690", "2668",
            "1f348", "1f3ac", "1f426", "1f47d", "1f4d2", "1f607", "1f691", "267b",
            "1f349", "1f3ad", "1f427", "1f47e", "1f4d3", "1f608", "1f692", "267f",
            "1f34a", "1f3ae", "1f428", "1f47f", "1f4d4", "1f609", "1f693", "2693",
            "1f34b", "1f3af", "1f429", "1f480", "1f4d5", "1f60a", "1f694", "26a0",
            "1f34c", "1f3b0", "1f42a", "1f481", "1f4d6", "1f60b", "1f695", "26a1",
            "1f34d", "1f3b1", "1f42b", "1f482", "1f4d7", "1f60c", "1f696", "26aa",
            "1f34e", "1f3b2", "1f42c", "1f483", "1f4d8", "1f60d", "1f697", "26ab",
            "1f34f", "1f3b3", "1f42d", "1f484", "1f4d9", "1f60e", "1f698", "26bd",
            "1f350", "1f3b4", "1f42e", "1f485", "1f4da", "1f60f", "1f699", "26be",
            "1f351", "1f3b5", "1f42f", "1f486", "1f4db", "1f610", "1f69a", "26c4",
            "1f352", "1f3b6", "1f430", "1f487", "1f4dc", "1f611", "1f69b", "26c5",
            "1f353", "1f3b7", "1f431", "1f488", "1f4dd", "1f612", "1f69c", "26ce",
            "1f354", "1f3b8", "1f432", "1f489", "1f4de", "1f613", "1f69d", "26d4",
            "1f355", "1f3b9", "1f433", "1f48a", "1f4df", "1f614", "1f69e", "26ea",
            "1f356", "1f3ba", "1f434", "1f48b", "1f4e0", "1f615", "1f69f", "26f2",
            "1f357", "1f3bb", "1f435", "1f48c", "1f4e1", "1f616", "1f6a0", "26f3",
            "1f358", "1f3bc", "1f436", "1f48d", "1f4e2", "1f617", "1f6a1", "26f5",
            "1f359", "1f3bd", "1f437", "1f48e", "1f4e3", "1f618", "1f6a2", "26fa",
            "1f35a", "1f3be", "1f438", "1f48f", "1f4e4", "1f619", "1f6a3", "26fd",
            "1f35b", "1f3bf", "1f439", "1f490", "1f4e5", "1f61a", "1f6a4", "2702",
            "1f35c", "1f3c0", "1f43a", "1f491", "1f4e6", "1f61b", "1f6a5", "2705",
            "1f35d", "1f3c1", "1f43b", "1f492", "1f4e7", "1f61c", "1f6a6", "2708",
            "1f35e", "1f3c2", "1f43c", "1f493", "1f4e8", "1f61d", "1f6a7", "2709",
            "1f35f", "1f3c3", "1f43d", "1f494", "1f4e9", "1f61e", "1f6a8", "270a",
            "1f360", "1f3c4", "1f43e", "1f495", "1f4ea", "1f61f", "1f6a9", "270b",
            "1f361", "1f3c6", "1f440", "1f496", "1f4eb", "1f620", "1f6aa", "270c",
            "1f362", "1f3c7", "1f442", "1f497", "1f4ec", "1f621", "1f6ab", "270f",
            "1f363", "1f3c8", "1f443", "1f498", "1f4ed", "1f622", "1f6ac", "2712",
            "1f364", "1f3c9", "1f444", "1f499", "1f4ee", "1f623", "1f6ad", "2714",
            "1f365", "1f3ca", "1f445", "1f49a", "1f4ef", "1f624", "1f6ae", "2716",
            "1f366", "1f3e0", "1f446", "1f49b", "1f4f0", "1f625", "1f6af", "2728",
            "1f367", "1f3e1", "1f447", "1f49c", "1f4f1", "1f626", "1f6b0", "2733",
            "1f368", "1f3e2", "1f448", "1f49d", "1f4f2", "1f627", "1f6b1", "2734",
            "1f369", "1f3e3", "1f449", "1f49e", "1f4f3", "1f628", "1f6b2", "2744",
            "1f36a", "1f3e4", "1f44a", "1f49f", "1f4f4", "1f629", "1f6b3", "2747",
            "1f36b", "1f3e5", "1f44b", "1f4a0", "1f4f5", "1f62a", "1f6b4", "274c",
            "1f36c", "1f3e6", "1f44c", "1f4a1", "1f4f6", "1f62b", "1f6b5", "274e",
            "1f36d", "1f3e7", "1f44d", "1f4a2", "1f4f7", "1f62c", "1f6b6", "2753",
            "1f36e", "1f3e8", "1f44e", "1f4a3", "1f4f9", "1f62d", "1f6b7", "2754",
            "1f36f", "1f3e9", "1f44f", "1f4a4", "1f4fa", "1f62e", "1f6b8", "2755",
            "1f370", "1f3ea", "1f450", "1f4a5", "1f4fb", "1f62f", "1f6b9", "2757",
            "1f371", "1f3eb", "1f451", "1f4a6", "1f4fc", "1f630", "1f6ba", "2764",
            "1f372", "1f3ec", "1f452", "1f4a7", "1f500", "1f631", "1f6bb", "2796",
            "1f373", "1f3ed", "1f453", "1f4a8", "1f501", "1f632", "1f6bc", "2797",
            "1f374", "1f3ee", "1f454", "1f4a9", "1f502", "1f633", "1f6bd", "27a1",
            "1f375", "1f3ef", "1f455", "1f4aa", "1f503", "1f634", "1f6be", "27b0",
            "1f376", "1f3f0", "1f456", "1f4ab", "1f504", "1f635", "1f6bf", "27bf",
            "1f377", "1f400", "1f457", "1f4ac", "1f505", "1f636", "1f6c0", "2934",
            "1f378", "1f401", "1f458", "1f4ad", "1f506", "1f637", "1f6c1", "2935",
            "1f379", "1f402", "1f459", "1f4ae", "1f507", "1f638", "1f6c2", "2b05",
            "1f37a", "1f403", "1f45a", "1f4af", "1f509", "1f639", "1f6c3", "2b50",
            "1f37b", "1f404", "1f45b", "1f4b0", "1f50a", "1f63a", "1f6c4", "2b55",
            "1f37c", "1f405", "1f45c", "1f4b1", "1f50b", "1f63b", "1f6c5",
            "1f380", "1f406", "1f45d", "1f4b2", "1f50c", "1f63c", "203c",
            "1f381", "1f407", "1f45e", "1f4b3", "1f50d", "1f63d", "2049",
            "1f382", "1f408", "1f45f", "1f4b4", "1f50e", "1f63e", "2122",
            "1f383", "1f409", "1f460", "1f4b5", "1f50f", "1f63f", "231a",
            "1f384", "1f40a", "1f461", "1f4b6", "1f510", "1f640", "231b",
            "1f385", "1f40b", "1f462", "1f4b7", "1f511", "1f645", "23f0",
    };

    public static Spannable convertToEmojiText(Context context, String text) {
        if (TextUtils.isEmpty(text))
            return new SpannableStringBuilder();
        SpannableStringBuilder spannable = new SpannableStringBuilder(text);
        spannableToEmoji(context, spannable);
        return spannable;
    }

    public static String codePointToString(String code) {
        return String.valueOf(Character.toChars(Integer.parseInt(code, 16)));
    }

    public static Drawable getDrawable(Context context, String hexStr) {
        for (String code : allEmojiCodePoints) {
            if (code.equals(hexStr)) {
                Integer id = idCache.get(hexStr);
                if (id == null)
                    id = ChatHelper.getInstance().getAppContext().getResources().getIdentifier("emoji_" + hexStr, "drawable", context.getPackageName());
                if (id == 0)
                    return null;
                Drawable drawable = ChatHelper.getInstance().getAppContext().getResources().getDrawable(id);
                if (drawable==null)
                    Log.i("ysc","drawable==null");
                idCache.put(hexStr, id);
                return drawable;
            }
        }
        return null;
    }

    public static void spannableToEmoji(Context context, Spannable spannable) {
        String text = spannable.toString();
        char[] ach = text.toCharArray();
        int len = ach.length;
        int count;
        String hexStr;

        for (int i = 0, cp; i < len; i += count) {
            cp = Character.codePointAt(ach, i);
            count = Character.charCount(cp);
            hexStr = Integer.toHexString(cp);

            Drawable emoji = getDrawable(context, hexStr);
            if (emoji == null)
                continue;

            emoji.setBounds(0, 0, emoji.getIntrinsicWidth() / 2, emoji.getIntrinsicHeight() / 2);
//            ImageSpan imageSpan = new ImageSpan(emoji, ImageSpan.ALIGN_BASELINE);// use ALIGN_BASELINE will cause mis-layout in multiline
            ImageSpan imageSpan = new ImageSpan(emoji);

            spannable.setSpan(imageSpan, i, i + count, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

    }

    public static Spannable convertToEmojiText(Context context, String text, String keyWord, int color) {
        if (TextUtils.isEmpty(text))
            return new SpannableStringBuilder();
        SpannableStringBuilder spannable = new SpannableStringBuilder(text);
        spannableToEmoji(context, spannable,keyWord,color);
        return spannable;
    }


    public static void spannableToEmoji(Context context, Spannable spannable, String keyWord, int color) {
        String text = spannable.toString();
        char[] ach = text.toCharArray();
        int len = ach.length;
        int count;
        String hexStr;

        for (int i = 0, cp; i < len; i += count) {
            cp = Character.codePointAt(ach, i);
            count = Character.charCount(cp);
            hexStr = Integer.toHexString(cp);

            Drawable emoji = getDrawable(context, hexStr);
            if (emoji == null)
                continue;

            emoji.setBounds(0, 0, emoji.getIntrinsicWidth() / 2, emoji.getIntrinsicHeight() / 2);
//            ImageSpan imageSpan = new ImageSpan(emoji, ImageSpan.ALIGN_BASELINE);// use ALIGN_BASELINE will cause mis-layout in multiline
            ImageSpan imageSpan = new ImageSpan(emoji);

            spannable.setSpan(imageSpan, i, i + count, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        if (text.indexOf(keyWord) != -1) {
            int index = text.indexOf(keyWord);
            int key_len = keyWord.length();
            spannable.setSpan(new ForegroundColorSpan(color), index,(index + key_len), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        }

    }



}
